<?php
class Controller{
	static $instance_model = null;
	private $config = array();
	static $ROOT = null;
	static $CONF = array();
	static $formrand = array();
	
	
	public function loadModel($modelname){
	$model_path = ROOT.'./application/models/'.$modelname.'.class.inc.php';
	require ROOT.'./config/db.configs.inc.php';
	$dbconfig = &$db;
	if(file_exists($model_path) && isset($model_path)) require_once $model_path;
	if(isset(self::$instance_model)) return self::$instance_model;
	else return self::$instance_model = new $modelname($dbconfig);
	}
	
//	function load($modelname){
//	if(!is_object($this->instance_model)) $this->instance_model = new $modelname();
//	return $this->instance_model;
//	}
	
	function loadView($tplname, $params = array(), $cust_dir = null, $cache = null, $content_type = 'text/html'){
	//	global $mtime, $_KCONF;
		require_once ROOT.'./result/smarty/Smarty.class.php';
		require_once ROOT.'./config/tlp.configs.inc.php';
		$config =& $tpl;
		$smarty = new Smarty();
		if(isset($cust_dir)){
			$smarty->template_dir = ROOT.$cust_dir;
			$smarty->compile_dir = $smarty->cache_dir = ROOT.$config['compile_dir'].'/'.urlencode($cust_dir);
		}else{
			$smarty->template_dir = ROOT."{$config['dir']}/{$config['theme']}/";
			$smarty->compile_dir = $smarty->cache_dir = ROOT . $config['compile_dir'];
		}
		skyIo::mkdir($smarty->compile_dir);
		$smarty->use_sub_dirs = false;
		$smarty->caching = isset($cache) ? $cache : $config['cache'];
		$smarty->cache_lifetime = $config['cache_lifetime'];
		$smarty->left_delimiter   = $config['left_delimiter'];
		$smarty->right_delimiter  = $config['right_delimiter'];
		$smarty->assign(is_array($params) ? $params : array()); // params
		$smarty->assign('sky_img', url::$webaddress.'/application/'); // root
		//$smarty->assign('sky_img', ROOT.'application/'); // root
        //  $smarty->assign('base_url', ROOT);
            $smarty->assign('base_url',url::$webaddress.'/');
            $smarty->assign('app_url',url::$webaddress.'./');
		$smarty->assign('ROOT',url::$webaddress);
		$smarty->assign('session_val',$_SESSION['admin']);
		// $smarty->assign('exectime', number_format(((float)strtok($nowtime = microtime(), ' ') + (float)strchr($nowtime, ' '))-$mtime, 6)); // exec time
		$smarty->assign('timeoffset', ($timeoffset>0?'+':($timeoffset<0?'-':'')).$timeoffset); // query num
		$content_type == 'text/html' && header("Content-type: text/html; charset=utf-8");
		$config['gzip'] && self::gzip();
		$smarty->display($tplname.$config['extend']);
		exit();
	}
	
	static function gzip($content = null){
		if(!isset($content)) return ob_start('sky::gzip');
		if(!headers_sent() && extension_loaded("zlib") && strstr($_SERVER["HTTP_ACCEPT_ENCODING"],"gzip")) {
		$content = gzencode($content,9);
		header("Content-Encoding: gzip"); 
		header("Vary: Accept-Encoding");
		header("Content-Length: ".strlen($content));
		}
		return $content;
	}	
	
	static function nocache(){
		headers_sent() && exit('Cannot modify header information, because headers already sent');
		header("Expires: Mon, 26 Jul 1970 00:00:00 GMT");
		header("Cache-Control: no-cache, must-revalidate");
		header("Pragma: no-cache");
	}
	
	function checkSaveCode($val,$key='savecode'){
		$result = $_SESSION[$key] === strtolower($val);
		return $result;
	}
	
	static function setFormRand($key=null){
		if(count(self::$formrand) == 0 && !is_array(self::$formrand = $_SESSION['formrand'])){
			self::$formrand = array();
		}
		self::$formrand[$key] = md5(time().request::ip(true).mt_rand());		
		$_SESSION['formrand'] = self::$formrand[$key];
		return self::$formrand[$key];
	}
	
	static function checkFromRand($key,$val){
		if($val == '' || !isset($val) || empty($val)) return false;
		if($_SESSION['formrand'] == '' || !isset($val)) return false;
		if($val !== self::$formrand[$key]) return false;
	}
	
     function message($meskey='default',$type='success',$jumpUrl='index',$intval=2,$target='_self'){
	    $mes_path = ROOT.'./config/message.configs.inc.php';
          if(isset($mes_path) && file_exists($mes_path)) require $mes_path;
          else exit('don\'t find '.$meskey.' in this config file');
		
          $meskey = $meskey ? $meskey : 'default';
          $message = $mess[$meskey]; 
          $jumpUrl = $jumpUrl ? $jumpUrl : 'index';
          $type = $type ? $type : 'success';
	    self::loadView('message',array('jumpUrl'=>$jumpUrl,'intval'=>$intval,'target'=>$target,'content'=>$message,'type'=>$type));
    }
    
	function __destruct(){
		$this->instance_model = null;
	}	
}
?>
